package leetcode.题库.二叉树;

import leetcode.题库.pubclass.TreeNode;

/**
 * description:
 *
 * @author 颜真明
 * @date 2023/6/7  15:36
 */
public class Solution617 {

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null && root2 == null) {
            return null;
        }
        int val;
        int val1 = root1 == null ? 0 : root1.val;
        int val2 = root2 == null ? 0 : root2.val;
        val = val1 + val2;
        TreeNode node = new TreeNode(val);
        if (root1 == null) {
            node.left = mergeTrees(null, root2.left);
            node.right = mergeTrees(null, root2.right);
            return node;
        }
        if (root2 == null) {
            node.left = mergeTrees(root1.left,null);
            node.right = mergeTrees(root1.right,null);
            return node;
        }
        node.left = mergeTrees(root1.left,root2.left);
        node.right = mergeTrees(root1.right,root2.right);
        return node;
    }

}
